############################################################################
################## Appendix
################## Herfindahl index
################## Plot results 
############################################################################

# import libraries
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

path_data = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final datasets'
path_charts = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final charts\Credit concentration\Herfindahl index\order by large firms\final'
if not os.path.isdir(path_charts):
    os.makedirs(path_charts)


# define plotting function
def plot_HHI(data, color, order):
    # data should be a dataframe
    # color should be a text
    # order should be a list of lists, where the first is order of all countries and the second is selected countries
        
    # single plots for selected countries
    sns.set(rc={'figure.figsize':(15,10), 'axes.labelsize':26, 'axes.titlesize':26,  'axes.labelpad':15, 'xtick.labelsize':26, 'ytick.labelsize':26}, style='white')
    for i, j in enumerate(['Large', 'Medium', 'Small', 'Micro']):
        plt.figure()
        sns.boxplot(data=data.loc[data['CNTRY'].isin(order[1])][data.loc[data['CNTRY'].isin(order[1])].sz_f == (i+1)], x='CNTRY', y='HHI', width=[0.6], whis=[25, 75], showfliers=False, showmeans=True, medianprops={'color':'red', 'linewidth':4}, meanprops={'markerfacecolor':'red', 'markeredgecolor':'red', 'markersize':12}, color=color, order=order[1]).set(xlabel='', ylabel='Herfindahl index')
        plt.savefig(path_charts + '\\Appendix_HHI_bysz_' + j + '_201912.pdf', dpi=600, bbox_inches='tight')

# load data
AC_final = pd.read_stata(path_data + '\dbtr_lvl_dt_f.dta')
AC_final.rename(columns={'cntry_dbtr':'CNTRY','nmbr_crdtrs':'NMBR_CRDTRS', 'hhi':'HHI'}, inplace=True)
AC_final.drop(AC_final[AC_final.nace_code == ''].index, inplace=True)

#order
order_cntry_alph = ['AT','BE','CY','DE','EE','ES','FI','FR','GR','IE','IT','LT','LU','LV','MT','NL','PT','SI','SK']
sel_countries_alph = ['AT','BE','DE','ES','FI','FR','GR','IE','IT','NL','PT']
order_cntry = AC_final[AC_final.sz_f == 1].groupby(['CNTRY'])['HHI'].mean().sort_values(ascending=False).index
order_slctd_cntry = order_cntry[order_cntry.isin(sel_countries_alph)]

plot_HHI(data=AC_final, color='#7ccaa5', order=[order_cntry, order_slctd_cntry])


